import ExelceEvent from "@/lib/ExelceEvent"
import Handsontable from 'handsontable';
export default class ExelceContext{
    constructor(worktable,designerRef){
       
        this.hot = worktable.hotInstance;
        this.exelceEvent = new ExelceEvent(designerRef,this);
        this.cellsMap=new Map();
        this.rowHeaders=[];
        let _this = this;

        Handsontable.hooks.add("afterRowResize",function(currentColumn, newSize){
            _this.exelceEvent.rowResizeEvent(currentColumn, newSize);
        },_this.hot);

        Handsontable.hooks.add("afterColumnResize",function(currentColumn, newSize){
            _this.exelceEvent.columnResizeEvent(currentColumn, newSize);
        },_this.hot);

        Handsontable.hooks.add("afterSelectionEnd",function(rowIndex,colIndex,row2Index,col2Index){
            _this.exelceEvent.selectionEvent(rowIndex,colIndex,row2Index,col2Index);
        },_this.hot);

    }
    addRowHeader(row,band){
        let targetHeader=null;
        for(let header of this.rowHeaders){
            if(header.rowNumber===row){
                targetHeader=header;
                break;
            }
        }
        if(targetHeader){
            targetHeader.band=band;
        }else{
            const newHeader={band,rowNumber:row};
            this.rowHeaders.push(newHeader);
        }
    }


    getCell(rowIndex,colIndex){
        let key=(rowIndex) + ',' + (colIndex);
        console.info(this.cellsMap)
        return this.cellsMap.get(key);
    }

    addCell(cell){
        let key=cell.y+','+cell.x;
        this.cellsMap.set(key,cell);
    }

    removeCell(cell){
        let key=cell.rowNumber+','+cell.columnNumber;
        this.cellsMap.delete(key);
    }

    deleteCell(rowNumber,columnNumber){
        let key=rowNumber+','+columnNumber;
        this.cellsMap.delete(key);
    }

    refreshCellWidth(colIndex,newSize){
        this.cellsMap.forEach(function(data, key, map){
            if(key.indexOf(","+colIndex)>0){
                data.value.width=newSize
            }
        })
    }

    refreshCellHeight(rowIndex,newSize){
        this.cellsMap.forEach(function(data, key, map){
            if(key.indexOf(rowIndex)==0){
                data.value.height=newSize
            }
        })
    }

    buildWidth(colIndex,colspan){
        let width=this.hot.getColWidth(colIndex);
        if(!colspan || colspan<2){
            return width;
        }
        let start=colIndex+1,end=colIndex+colspan;
        for(let i=start;i<end;i++){
            width+=this.hot.getColWidth(i);
        }
        return width;
    }

    buildHeight(rowIndex,rowspan){
        let height=this.hot.getRowHeight(rowIndex);
        if(!rowspan || rowspan<2){
            return height;
        }
        let start=rowIndex+1,end=rowIndex+rowspan;
        for(let i=start;i<end;i++){
            height+=this.hot.getRowHeight(i);
        }
        return height;
    }

    getSelectedCells(){
        const selected=this.hot.getSelected();
        if(!selected){
            return null;
        }
        const startRow=selected[0], startCol=selected[1], endRow=selected[2], endCol=selected[3];
        const cells=[];
        for(let i=startRow;i<=endRow;i++){
            for(let j=startCol;j<=endCol;j++){
                const cell=this.hot.getCell(i,j,true);
                const exist=cells.indexOf(cell);
                if(exist===-1){
                    cells.push(cell);
                }
            }
        }
        return cells;
    }
}